package com.evan.review.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.evan.review.R;

/**
 * 生命周期演示Fragment
 * 记录所有生命周期方法的调用
 */
public class LifecycleFragment extends Fragment {

    private static final String TAG = "LifecycleFragment";
    private TextView tvFragmentInfo;
    private BasicLifecycleActivity parentActivity;
    
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof BasicLifecycleActivity) {
            parentActivity = (BasicLifecycleActivity) context;
        }
        logLifecycleEvent("onAttach()");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        logLifecycleEvent("onCreate() - savedInstanceState: " + (savedInstanceState == null ? "null" : "not null"));
        
        // 可以在这里恢复状态
        if (savedInstanceState != null) {
            // 从Bundle恢复数据
            logLifecycleEvent("从savedInstanceState恢复数据");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        logLifecycleEvent("onCreateView()");
        return inflater.inflate(R.layout.fragment_lifecycle, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        logLifecycleEvent("onViewCreated()");
        
        tvFragmentInfo = view.findViewById(R.id.tv_fragment_info);
        updateInfo("Fragment已创建 - 这是LifecycleFragment");
    }
    
    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        logLifecycleEvent("onViewStateRestored()");
    }

    @Override
    public void onStart() {
        super.onStart();
        logLifecycleEvent("onStart()");
    }

    @Override
    public void onResume() {
        super.onResume();
        logLifecycleEvent("onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        logLifecycleEvent("onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        logLifecycleEvent("onStop()");
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        logLifecycleEvent("onSaveInstanceState()");
        
        // 可以在这里保存状态
        // outState.putString("key", "value");
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        logLifecycleEvent("onDestroyView()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        logLifecycleEvent("onDestroy()");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        logLifecycleEvent("onDetach()");
        parentActivity = null;
    }
    
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        logLifecycleEvent("setUserVisibleHint() - isVisibleToUser: " + isVisibleToUser);
    }
    
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        logLifecycleEvent("onHiddenChanged() - hidden: " + hidden);
    }
    
    /**
     * 记录生命周期事件
     */
    private void logLifecycleEvent(String event) {
        String message = "[Fragment] " + event + " [" + this.hashCode() + "]";
        Log.d(TAG, message);
        
        if (parentActivity != null) {
            parentActivity.appendFragmentLog(message);
        }
    }
    
    /**
     * 更新Fragment信息显示
     */
    private void updateInfo(String info) {
        if (tvFragmentInfo != null) {
            tvFragmentInfo.setText(info);
        }
    }
    
    /**
     * 清空日志（由Activity调用）
     */
    public void clearLog() {
        logLifecycleEvent("日志已清空");
    }
} 